package com.gitee.aurora.server.dao.impl;


import com.gitee.aurora.core.comm.AuroraJdbcTemplate;
import com.gitee.aurora.server.dao.SjjcLogInfoDao;
import com.gitee.aurora.server.vo.SjjcLogInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Repository
public class SjjcLogInfoDaoImpl implements SjjcLogInfoDao {
    @Autowired
    private AuroraJdbcTemplate auroraJdbcTemplate;
    @Override
    public int insertSjjcLogInfo(SjjcLogInfo sjjcLogInfo) {
        String sql = "insert into  TAB_SJJC_LOG_INFO (S_ID,S_CODE,MESSAGE,STATUS,CREATE_TIME,UPDATE_TIME) values (?,?,?,?,?,?)";

        return auroraJdbcTemplate.update(sql, new Object[]{UUID.randomUUID().toString(),sjjcLogInfo.getCode(),sjjcLogInfo.getMessage(),sjjcLogInfo.getStatus(),new Date(),new Date()});

    }

    @Override
    public List<SjjcLogInfo> querySjjcLogInfo(SjjcLogInfo sjjcLogInfo) throws Exception {
        List<Object> params = new ArrayList<>();
        int[] types = new int[]{java.sql.Types.VARCHAR, java.sql.Types.VARCHAR,java.sql.Types.VARCHAR};
        StringBuilder sql = new StringBuilder("select S_CODE sCode,MESSAGE message,STATUS status,DATE_FORMAT(CREATE_TIME,'%Y-%m-%d %H:%i:%s') AS createTime from TAB_SJJC_LOG_INFO WHERE 1=1 ");
        if (sjjcLogInfo != null) {
            if (sjjcLogInfo.getCode() != null) {
                sql.append("and S_CODE=?");
                params.add(sjjcLogInfo.getCode());
            }
            if (sjjcLogInfo.getCreateTime() != null) {
                sql.append(" and    CREATE_TIME BETWEEN DATE_ADD( ? , interval -1 hour) AND ? ");
                params.add(sjjcLogInfo.getCreateTime());
                params.add(sjjcLogInfo.getCreateTime());
          }
            sql.append(" ORDER BY CREATE_TIME DESC LIMIT 1");

        }
        return auroraJdbcTemplate.queryForMapToList(sql.toString(),params.toArray() ,types, SjjcLogInfo.class);
    }
}
